package com.zjkj.mp.controller.accountManage;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zjkj.mp.service.user.UserService;
import com.zjkj.mp.utils.security.MD5Encrypt;
import com.zjkj.mp.vo.user.MpUser;

/**
 * 
 * 修改资料控制器
 *
 * @author 陈晨
 * @history
 * <TABLE id="HistoryTable" border="1">
 * 	<TR><TD>时间</TD><TD>描述</TD><TD>作者</TD></TR>
 *	<TR><TD>2013-6-9</TD><TD>创建初始版本</TD><TD>陈晨</TD></TR>
 * </TABLE>
 */
@Controller
public class ModifyInfoController {

	@Resource(name = "userServiceImpl")
	private UserService userService;

	/**
	 * 显示用户自己修改账户资料的界面
	 */
	@RequestMapping("/modifyAccountInfo.do")
	public String modifyInfo(HttpServletRequest req){
		SecurityContext ctx=  SecurityContextHolder.getContext();
		MpUser mpUser = (MpUser) ctx.getAuthentication().getPrincipal();
		mpUser = this.userService.findUserById(mpUser.getUserId());
		
		req.setAttribute("user", mpUser);		
		return "account/modifyUserInfo";
	}
	
	/**
	 * 保存用户修改的信息
	 */
	@RequestMapping("/doModifyUserInfo.do")
	public String  doModifyUserInfo(HttpServletRequest req){
		//得到新建用户信息
				String realName = req.getParameter("realName");
				String mobile = req.getParameter("mobile");
				String phone = req.getParameter("phone");
				String birthday = req.getParameter("birthday");
				String unit = req.getParameter("unit");
				String address = req.getParameter("address");
				String sex = req.getParameter("sex");
				String userId = req.getParameter("id");
				String errorMsg = null;
				 
				//检查用户ID是否存在
				if(userId == null){
					errorMsg = "用户信息不存在，保存用户信息失败！";
					req.setAttribute("errorMsg", errorMsg);
				}else{
					//组装对象
					MpUser user  = this.userService.findUserById(Integer.parseInt(userId));
					if(user != null){
						user.setRealName(realName);
						user.setMobile(mobile);
						user.setPhone(phone);
						user.setBirthDay(birthday);
						user.setUnit(unit);
						user.setAddress(address);
						user.setSex(sex);
					
						//开始更新
						this.userService.updateInfo(user);
						req.setAttribute("promptMsg", "信息修改成功！");
						req.setAttribute("user", user);
					} 
				}			 

				//返回详细信息的界面		
				return "account/modifyUserInfo";	
		}
	
	/**
	 * 显示修改密码的界面
	 */
	@RequestMapping("/chanagepwd.do")
	public String modifyPassword(HttpServletRequest req){
				
		return "account/changepwd";
	}
	
	@RequestMapping("/doChangePSW.do")
	public String  doModifyPSW(HttpServletRequest req){
		String oldPSW = req.getParameter("oldPSW");
		String password = req.getParameter("newPSW");
		
		//得到当前用户
		SecurityContext ctx=  SecurityContextHolder.getContext();
		MpUser mpUser = (MpUser) ctx.getAuthentication().getPrincipal();
		mpUser = this.userService.findUserById(mpUser.getUserId());
		
		//校验原密码
		if(MD5Encrypt.encode(oldPSW).equals(mpUser.getPassword())){
			//可以修改
			mpUser.setPassword(MD5Encrypt.encode(password));
			this.userService.updateInfo(mpUser);
			req.setAttribute("msg", "修改密码成功！");
		}else{
			req.setAttribute("msg", "修改失败，原始密码错误！");
		}
		
		return "account/result";
	}
}
